
package com.chrray.position.aop;

import com.alibaba.fastjson.JSONObject;
import com.chrray.position.auth.Authentication;
import com.chrray.position.auth.AuthenticationContext;
import com.chrray.position.entity.position.SysUser;
import com.chrray.position.mapper.position.SysUserMapper;
import com.chrray.position.pojo.dto.util.BaseAllDto;
import com.chrray.position.util.redis.RedisUtils;
import com.chrray.position.util.request.RequestUtils;
import com.chrray.position.util.result.ResultEnum;
import org.apache.commons.lang3.ObjectUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import static com.chrray.position.util.result.ResponseResults.fail;

@Aspect
@Component
public class LoginUserAspect {
    private static final Logger LOGGER = LoggerFactory.getLogger(LoginUserAspect.class);

    @Autowired
    private RedisUtils redisUtils;
    @Resource
    private SysUserMapper sysUserMapper;


    @Pointcut("execution(public * com.chrray.position.controller..*.*(..))")
    public void annotationPointCut() {
    }

    @After("annotationPointCut()")
    public void after() {
        AuthenticationContext.clear();
    }

    @Around("annotationPointCut()")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        if(!request.getRequestURL().toString().contains("/api")
                ||request.getRequestURL().toString().contains("/login")||
                request.getRequestURL().toString().contains("/upload")||
                request.getRequestURL().toString().contains("/download")||
                request.getRequestURL().toString().contains("/payNotice")||
                request.getRequestURL().toString().contains("/isUp")||
                //request.getRequestURL().toString().contains("/app")||
//                request.getRequestURL().toString().contains("/web")||
                request.getRequestURL().toString().contains("/refundNotice")){
            return   pjp.proceed();
        }
        //获取token
        String token = RequestUtils.getToken(request);
        if(ObjectUtils.isEmpty(token)){
            return  fail(ResultEnum.USER_TOKEN_ERROR);
        }
        SysUser sysUser =null;
        if(token.startsWith("webUser_")){
             sysUser = this.getUserId(token);
            if(ObjectUtils.isEmpty(sysUser)){
                return  fail(ResultEnum.USER_TOKEN_ERROR);
            }
        }else {
            //小程序端
            boolean success = AuthenticationContext.restoreContext(token);
            if (!success) {
                return fail(ResultEnum.USER_TOKEN_ERROR);
            }
        }




        for (Object object : pjp.getArgs()) {
            if (object instanceof BaseAllDto) {
                final BaseAllDto backendBaseDTO = (BaseAllDto)object;
                if(!ObjectUtils.isEmpty(sysUser)){
                    backendBaseDTO.setLoginSysUserId(sysUser.getId());
                    backendBaseDTO.setLoginSysUserName(sysUser.getUserName());
                    backendBaseDTO.setLoginRoleId(sysUser.getRoleId());
                    backendBaseDTO.setLoginIsManage(sysUser.getIsManage());
                }
                //小程序端 TODO
            }
        }
        Object result = pjp.proceed();
        return result;

    }



    public  SysUser getUserId( String token ){
        Object sysUser=redisUtils.get(token);
        if(ObjectUtils.isEmpty(sysUser)){
            return null;
        }
        return JSONObject.parseObject(sysUser.toString(),SysUser.class);
    }
}



